package com.aionengine.gameserver.utils.chathandlers;

import com.aionemu.commons.scripting.classlistener.ClassListener;
import com.aionemu.commons.utils.ClassUtils;

import java.lang.reflect.Modifier;

/**
 * Created on: 12.09.2009 14:13:24
 *
 * @author Aquanox
 */
public class ChatCommandsLoader implements ClassListener {

    private ChatProcessor processor;

    public ChatCommandsLoader(ChatProcessor processor) {
        this.processor = processor;
    }

    @Override
    public void postLoad(Class<?>[] classes) {
        for (Class<?> c : classes) {
            if (!isValidClass(c))
                continue;
            Class<?> tmp = (Class<?>) c;
            if (tmp != null)
                try {
                    processor.registerCommand((ChatCommand) tmp.newInstance());
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
        }
        processor.onCompileDone();
    }

    @Override
    public void preUnload(Class<?>[] classes) {

    }

    public boolean isValidClass(Class<?> clazz) {
        final int modifiers = clazz.getModifiers();

        if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers))
            return false;

        if (!Modifier.isPublic(modifiers))
            return false;

        if (!ClassUtils.isSubclass(clazz, AdminCommand.class) && !ClassUtils.isSubclass(clazz, PlayerCommand.class)
                && !ClassUtils.isSubclass(clazz, WeddingCommand.class))
            return false;
        return true;
    }
}
